package goko.simple.api.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.lsl.rdf.annotation.InjectEnumResult;
import com.lsl.rdf.errCode.ErrorCode;
import com.lsl.rdf.result.BaseResult;
import com.lsl.rdf.result.page.PageResult;
import com.lsl.rdf.tuple.Tuple;
import com.lsl.rdf.utils.bean.IBeanUtil;
import goko.simple.GoodsDo;
import goko.simple.api.GoodsServiceRest;
import goko.simple.contract.*;
import goko.simple.enums.GoodsStatusEnum;
import goko.simple.enums.GoodsTypeEnum;
import goko.simple.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Objects;

/**
 * Created by lsl on 2021/7/9.
 */
@Slf4j
@Service
public class GoodsServiceRestImpl implements GoodsServiceRest {

    @Autowired
    private GoodsService goodsService;

    @Override
    public BaseResult<GoodsCreateResult> goodsCreateRest(GoodsCreateParam createParam) {
        if (Objects.isNull(GoodsStatusEnum.getByVal(GoodsStatusEnum.class, createParam.getStatus()))) {
            // 普通错误响应
            return BaseResult.error("商品状态错误");
        }
        if (Objects.isNull(GoodsTypeEnum.getByCode(GoodsTypeEnum.class, createParam.getType()))) {
            // 带子码错误响应
            return BaseResult.error("500000", "商品类型错误");
        }
        // 复制实例
        GoodsDo newGoodsDo = IBeanUtil.copyInstance(createParam, GoodsDo::new);
        if (goodsService.insert(newGoodsDo) > 0) {
            GoodsDo goodsDo = goodsService.getById(newGoodsDo.getId());
            return BaseResult.success(IBeanUtil.copyInstance(goodsDo, GoodsCreateResult::new));
        } else {
            return BaseResult.error(ErrorCode.ADDITION_FAILED);
        }
    }

    /**
     * 普通列表查询
     */
    @Override
    public BaseResult<Tuple<List<GoodsSearchResult>>> goodsSearchRest(GoodsSearchParam searchParam) {
        GoodsDo searchGoodsDo = IBeanUtil.copyInstance(searchParam, GoodsDo::new);
        // 查询商品列表
        List<GoodsDo> list = goodsService.list(new QueryWrapper<>(searchGoodsDo));
        // 列表对象复制，并转换为响应对象，回调函数的用法
        List<GoodsSearchResult> searchResults = IBeanUtil.copyListProperties(list, GoodsSearchResult::new,
                (source, target) -> {
                    // 回调方法
                    target.setStatusDesc(GoodsStatusEnum.getDescByVal(GoodsStatusEnum.class, source.getStatus()));
                    target.setTypeDesc(GoodsTypeEnum.getDescByCode(GoodsTypeEnum.class, source.getType()));
                });
        return BaseResult.success(Tuple.of(searchResults));
    }

    /**
     * 分页查询
     */
    @Override
    @InjectEnumResult
    public BaseResult<PageResult<GoodsPageSearchResult>> goodsPageRest(GoodsPageSearchParam pageParam) {
        if (Objects.isNull(pageParam.getPageBegin()) || Objects.isNull(pageParam.getPageSize())) {
            pageParam.setPageBegin(1);
            pageParam.setPageSize(10);
        }
        GoodsDo pageGoodsDo = IBeanUtil.copyInstance(pageParam, GoodsDo::new);
        // 分页查询
        PageInfo<GoodsDo> pageInfo = goodsService.page(pageGoodsDo);
        // 封装分页查询结果
        return BaseResult.success(PageResult.of(pageInfo, GoodsPageSearchResult::new, (s, t) -> {
            // 这里是回调方法...
        }));
    }

    @Override
    public BaseResult<Boolean> goodsUpdateRest(GoodsUpdateParam updateParam) {
        GoodsDo goodsDo = goodsService.selectOne(GoodsDo.builder().id(updateParam.getId()).build());
        if (Objects.isNull(goodsDo)) {
            return BaseResult.error(ErrorCode.NO_RESOURCE_AVAILABLE);
        }
        GoodsDo updateGoodsDo = IBeanUtil.copyInstance(updateParam, GoodsDo::new);
        updateGoodsDo.setVersion(goodsDo.getVersion());
        if (goodsService.update(updateGoodsDo) > 0) {
            return BaseResult.success();
        }
        return BaseResult.success(Boolean.FALSE);
    }
}
